package com.codefish.codefishseckill.aop;

import com.codefish.codefishseckill.aop.annotation.EnablePage;
import com.codefish.codefishseckill.exception.GlobalException;
import com.codefish.codefishseckill.utils.PageUtils;
import com.codefish.codefishseckill.utils.model.PageInfo;
import com.codefish.codefishseckill.vo.RespBeanFactory;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 分页Aspect
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/09/01 下午 03:42
 */
@Aspect
@Component
@Slf4j
public class PageHelperAspect {

    @Pointcut("@annotation(com.codefish.codefishseckill.aop.annotation.EnablePage)")
    public void pointCut() {

    }

    @Around(value = "pointCut() && @annotation(page)")
    public String beforeAdvise(ProceedingJoinPoint pjp, EnablePage page) {
        try {
            PageInfo pageInfo = (PageInfo) pjp.getArgs()[1];
            PageUtils.countFromIndex(pageInfo);
            String result = (String) pjp.proceed();
//            log.info("{}", pageInfo);
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            throw new GlobalException(RespBeanFactory.fail("查询失败"));
        }
    }


}
